package com.dbsop.domain.params;

import com.dbsop.common.util.EsFieldUtils;
import com.dbsop.domain.EsCondition;
import com.dbsop.domain.info.EsFiledInfo;
import com.dbsop.domain.info.Params;
import com.dbsop.mapper.IndexMappingInfo;
import com.dbsop.wrapper.DataWrapper;
import lombok.Getter;
import lombok.Setter;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;

import java.util.Map;

@Getter
@Setter
public class PageParams<T> implements Params {

    private EsCondition esCondition = EsCondition.getEsCondition();

    private Class<T> resClass;

    private IndexMappingInfo indexInfo;

    private DataWrapper<T> dataWrapper;

    private ESPage page;

    public PageParams(EsCondition esCondition, Class<T> resClass, IndexMappingInfo indexInfo,ESPage page) {
        this.esCondition = esCondition;
        this.resClass = resClass;
        this.indexInfo = indexInfo;
        this.page = page;
        indexHandler();
    }

    public PageParams(EsCondition esCondition, IndexMappingInfo indexInfo,ESPage page, DataWrapper<T> dataWrapper) {
        this.esCondition = esCondition;
        this.indexInfo = indexInfo;
        this.dataWrapper = dataWrapper;
        this.page = page;
        indexHandler();
    }


    public Map<String, EsFiledInfo> getFieldMapping(){
        if (resClass!=null){
            return  EsFieldUtils.pareFieldEsInfo(resClass);
        }
        return null;
    }


    @Override
    public String[] getIndex() {
        return  esCondition.getIndices();
    }

    @Override
    public void indexHandler() {
        esCondition.size(page.getCurrentSize());
        esCondition.from(page.getFrom());
        String[] indices = esCondition.getIndices();
        if (indices !=null && indices.length>0){
            return ;
        }
        esCondition.addIndex(indexInfo.queryIndex());
    }
}
